package com.zbkj.admin.controller.platform;


import cn.hutool.core.util.StrUtil;
import com.zbkj.admin.boot.redisson.command.Redis;
import com.zbkj.admin.constants.SmsConstants;
import com.zbkj.admin.page.CommonPage;
import com.zbkj.admin.request.*;
import com.zbkj.admin.response.UserAdminDetailResponse;
import com.zbkj.admin.response.UserResponse;
import com.zbkj.admin.result.CommonResult;
import com.zbkj.admin.service.UserService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import net.dreamlu.mica.core.result.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * 平台端用户控制器
 * +----------------------------------------------------------------------
 * | CRMEB [ CRMEB赋能开发者，助力企业发展 ]
 * +----------------------------------------------------------------------
 * | Copyright (c) 2016~2025 https://www.crmeb.com All rights reserved.
 * +----------------------------------------------------------------------
 * | Licensed CRMEB并不是自由软件，未经许可不能去掉CRMEB相关版权
 * +----------------------------------------------------------------------
 * | Author: CRMEB Team <admin@crmeb.com>
 * +----------------------------------------------------------------------
 */
@Slf4j
@RestController
@RequestMapping("api/admin/platform/user")
@Tag(name = "平台端用户控制器")
@Validated
public class PlatformUserController {
    @Autowired
    private UserService userService;
    @Autowired
    private Redis redis;

    @PreAuthorize("hasAuthority('platform:user:page:list')")
    @Operation(summary = "平台端用户分页列表")
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public CommonResult<CommonPage<UserResponse>> getList(@ModelAttribute @Validated UserSearchRequest request) {
        CommonPage<UserResponse> userCommonPage = CommonPage.restPage(userService.getPlatformPage(request));
        return CommonResult.success(userCommonPage);
    }

    @PreAuthorize("hasAuthority('platform:user:update')")
    @Operation(summary = "修改用户信息")
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public CommonResult<String> update(@RequestBody @Validated UserUpdateRequest userRequest) {
        if (userService.updateUser(userRequest)) {
            return CommonResult.success();
        }
        return CommonResult.failed();
    }

    @PreAuthorize("hasAuthority('platform:user:detail')")
    @Operation(summary = "用户详情")
    @RequestMapping(value = "/detail/{id}", method = RequestMethod.GET)
    public CommonResult<UserAdminDetailResponse> detail(@PathVariable(value = "id") Integer id) {
        return CommonResult.success(userService.getAdminDetail(id));
    }

    @PreAuthorize("hasAuthority('platform:user:tag')")
    @Operation(summary = "用户分配标签")
    @RequestMapping(value = "/tag", method = RequestMethod.POST)
    public CommonResult<String> tag(@RequestBody @Validated UserAssignTagRequest request) {
        if (userService.tag(request)) {
            return CommonResult.success();
        }
        return CommonResult.failed();
    }

    @PreAuthorize("hasAuthority('platform:user:operate:integer')")
    @Operation(summary = "操作用户积分")
    @RequestMapping(value = "/operate/integer", method = RequestMethod.GET)
    public CommonResult<Object> founds(@Validated UserOperateIntegralRequest request) {
        if (userService.operateUserInteger(request)) {
            return CommonResult.success();
        }
        return CommonResult.failed();
    }

    @PreAuthorize("hasAuthority('platform:user:operate:balance')")
    @Operation(summary = "操作用户余额")
    @RequestMapping(value = "/operate/balance", method = RequestMethod.GET)
    public R<Object> founds(@Validated UserOperateBalanceRequest request) {
//        String codeOne = redis.get(SmsConstants.SMS_VALIDATE_PHONE + "19520730042");
//        String codeTwo = redis.get(SmsConstants.SMS_VALIDATE_PHONE + "18927559908");
//        String codeThree = redis.get(SmsConstants.SMS_VALIDATE_PHONE + "18680449909");
        String codeOne = redis.get(SmsConstants.SMS_VALIDATE_PHONE + "19520730042");
        String codeTwo = redis.get(SmsConstants.SMS_VALIDATE_PHONE + "13824236907");
        String codeThree = redis.get(SmsConstants.SMS_VALIDATE_PHONE + "18520535663");
        R validatePhone = validatePhone(codeOne, request.getPhoneOneCode());
        validatePhone.setCode(200);
        if (validatePhone.isSuccess()) {
            R phone = validatePhone(codeTwo, request.getPhoneTwoCode());
            phone.setCode(200);
            if (phone.isSuccess()) {
                R phoneThree = validatePhone(codeThree, request.getPhoneThreeCode());
                phoneThree.setCode(200);
                if (phoneThree.isSuccess()) {
                    if (userService.operateUserBalance(request)) {
                        R<Object> success = R.success();
                        success.setCode(200);
                        return success;
                    }
                    return phoneThree;
                } else {
                    phoneThree.setCode(500);
                    return phoneThree;
                }
            } else {
                phone.setCode(500);
                return phone;
            }
        }

        R<Object> failed = R.fail("修改失败");
        failed.setCode(500);
        return failed;
    }

    public R validatePhone(String code, String reqcode) {
        if (StrUtil.isBlank(code)) {
            return R.fail("验证码已过期");
        }
        if (!code.equalsIgnoreCase(reqcode)) {
            return R.fail("验证码错误");
        }
        return R.success();
    }

    @PreAuthorize("hasAuthority('platform:user:gift:paid:member')")
    @Operation(summary = "赠送用户付费会员")
    @RequestMapping(value = "/gift/paid/member", method = RequestMethod.POST)
    public CommonResult<String> giftPaidMember(@RequestBody @Validated GiftPaidMemberRequest request) {
        if (userService.giftPaidMember(request)) {
            return CommonResult.success();
        }
        return CommonResult.failed();
    }
}



